/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/view/draw_generic_game.h"
#include "PlanetWars2dRT-core/specific/model/universe_planets.h"
#include "PlanetWars2dRT-core/specific/model/universe_ships.h"


EXTERN_C_BEGIN


bool
generic_draw_planets_of_game(const game_t*                a_game,
			     generic_draw_planet_function draw_planet,
			     const void*                  extra_data,
			     bool                         stop_on_error)
{
  bool success = true;
  const size_t nb = universe_get_nb_planets(&a_game->universe);
  for(size_t i=0; i < nb; ++i)
    {
      const planet_t* a_planet =
	universe_get_const_planet_no_check(&a_game->universe, i);
      if(!draw_planet(a_planet, extra_data))
	{
	  if(stop_on_error)
	    {
	      return false;
	    }
	  else
	    {
	      success = false;
	    }
	}
    }
  return success;
}

bool
generic_draw_ships_of_game(const game_t*              a_game,
			   generic_draw_ship_function draw_ship,
			   const void*                extra_data,
			   bool                       stop_on_error)
{
  bool success = true;
  const size_t nb = universe_get_nb_ships(&a_game->universe);
  for(size_t i=0; i < nb; ++i)
    {
      const ship_t* a_ship = universe_get_const_ship_no_check(&a_game->universe, i);
      if(!draw_ship(a_ship, extra_data))
	{
	  if(stop_on_error)
	    {
	      return false;
	    }
	  else
	    {
	      success = false;
	    }
	}
    }
  return success;
}

bool
generic_draw_game(const game_t*                       a_game,
		  const generic_draw_game_functions*  functions,
		  const generic_draw_game_extra_data* extra_data,
		  bool                                stop_on_error)
{
  bool success = true;
  
  if(!generic_draw_planets_of_game(a_game,
				   functions->planet,
				   extra_data->planets,
				   stop_on_error))
    {
      if(stop_on_error)
	{
	  return false;
	}
      else
	{
	  success = false;
	}
    }
  
  if(!generic_draw_ships_of_game(a_game,
				 functions->ship,
				 extra_data->ships,
				 stop_on_error))
    {
      if(stop_on_error)
	{
	  return false;
	}
      else
	{
	  success = false;
	}
    }
  
  return success;
}


EXTERN_C_END
